package matchthreegame.serverEvents;


import java.util.List;

import matchthreegame.extensions.MatchThreeGameRoomExtension;

import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.core.SFSEventType;
import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;

public class OnUserLeaveGameRoomHandler extends BaseServerEventHandler {
	
	@Override
	public void handleServerEvent(ISFSEvent event) throws SFSException {
		
		if ( event.getType() == SFSEventType.USER_DISCONNECT ) {
			trace("### USER_DISCONNECT");
			
			List<Room> joinedRooms = (List<Room>)event.getParameter(SFSEventParam.JOINED_ROOMS);
			User user =  (User) event.getParameter(SFSEventParam.USER);
			
			trace("user: " + user);
			
			if ( joinedRooms.size() > 0 && joinedRooms.get(0) != null && joinedRooms.get(0).isGame() == true ) {
				
				MatchThreeGameRoomExtension roomExtension = (MatchThreeGameRoomExtension) joinedRooms.get(0).getExtension();
				roomExtension.userLeftTheGame(user.getId());
				
			}
			
		}
		
	}

}
